package com.leetcode.partition4;

/**
 * @author `RKC`
 * @date 2021/9/9 18:28
 */
public class LC383赎金信 {

    public static boolean canConstruct(String ransomNote, String magazine) {
        return hashtable(ransomNote.toCharArray(), magazine.toCharArray());
    }

    public static void main(String[] args) {
        String ransomNote = "aa", magazine = "ab";
        System.out.println(canConstruct(ransomNote, magazine));
    }

    private static boolean hashtable(final char[] ransomNotes, final char[] magazines) {
        int[] ht = new int[26];
        //构建哈希表
        for (int i = 0; i < magazines.length; i++) ht[magazines[i] - 'a']++;
        for (int i = 0; i < ransomNotes.length; i++) {
            if (ht[ransomNotes[i] - 'a'] > 0) ht[ransomNotes[i] - 'a']--;
            else return false;
        }
        return true;
    }
}
